package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.common.minio.service.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.media.service.IWmNewsMaterialService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2021-12-23
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;


    @Autowired
    private IWmNewsMaterialService newsMaterialService;
    

    /**
     * 1.将图片文件上传到MinIO  2.将上传后图片地址作为素材关联到当前用户
     *
     * @param file 上传文件
     * @return 图片在线地址
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        //0.校验用户信息是否合法
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.将图片文件上传到MinIO
        String upload = minIOService.upload(file);

        //2.保存素材记录
        //2.1 构建素材对象 素材对象属性赋值
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getUserId());
        wmMaterial.setUrl(upload);
        wmMaterial.setType(MediaEnum.MEDIA_MATERIAL_TYPE_IMG.getVal());
        wmMaterial.setIsCollection(false);

        //2.2 保存返回素材对象
        this.save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 分页查询当前用户上传的素材列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        //0.校验用户信息是否合法
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.封装查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());
        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //2.执行分页查询
        IPage<WmMaterial> iPage = this.page(new Page<>(dto.getPage(), dto.getSize()), queryWrapper);

        //3.处理响应结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 修改素材收藏状态
     * @param id 素材ID
     * @param type 1:添加收藏  0:取消收藏
     * @return
     */
    @Override
    public ResponseResult updateCollect(Integer id, int type) {
        // 更新素材表的状态
        LambdaUpdateWrapper<WmMaterial> update = new LambdaUpdateWrapper<>();
        // 指定更新的条件,相当于where 后面的条件
        update.eq(WmMaterial::getId, id);
        // 更新字段,相当于 set
        update.set(WmMaterial::getIsCollection, type);
        this.update(update);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        // 根据id查询素材
        WmMaterial material = this.getById(id);
        if (material == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 判断素材是否被文章引用
        LambdaQueryWrapper<WmNewsMaterial> query = new LambdaQueryWrapper<>();
        query.eq(WmNewsMaterial::getMaterialId, id);
        int count = newsMaterialService.count(query);
        // 如果被引用,提示不能删除
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        }
        // 删除素材
        this.removeById(id);
        // 调用minio服务删除远程的对象
        minIOService.delete(material.getUrl());
        return ResponseResult.okResult();
    }
}
